1 // Fig. 12.4: fig12_04.cpp 2 // Test driver for Stack template. 3 // Function main uses a function template to manipulate 4 // objects of type Stack< T >. 5 #include 6 #include "tstack1.h" 7 8 // Function template to manipulate Stack< T > 9 template< class T > 10 void testStack( 11 Stack< T > &theStack, // reference to the Stack< T > 12 T value, // initial value to be pushed 13 T increment, // increment for subsequent values 14 const char *stackName ) // name of the Stack< T > object 15 { 16 cout << "\nPushing elements onto " << stackName << '\n'; 17 18 while ( theStack.push( value ) ) { // success true returned 19 cout << value << ' '; 20 value += increment; 21 } 22 23 cout << "\nStack is full. Cannot push " << value 24 << "\n\nPopping elements from " << stackName << '\n'; 25 26 while ( theStack.pop( value ) ) // success true returned 27 cout << value << ' '; 28 29 cout << "\nStack is empty. Cannot pop\n"; 30 } 31 32 int main() 33 { 34 Stack< double > doubleStack( 5 ); 35 Stack< int > intStack; 36 37 testStack( doubleStack, 1.1, 1.1, "doubleStack" ); 38 testStack( intStack, 1, 1, "intStack" ); 39 40 return 0; 41 }